#include "project.h"



void *pthread_M0_ctrl()
{
	struct msg msg_buf = {0, {0}};
	key_t key; 
	
	int msgid; 
	char device[6] = {0};

	memset(&msg_buf, 0, sizeof(msg_buf));
	int dev_uart_fd;
	if((dev_uart_fd = open("/dev/ttyUSB0", O_RDWR)) < 0)
	{
			perror("uart uart err");
			exit(1);
	}
	serial_init(dev_uart_fd);

	if((key = ftok("/", 'b')) < 0)
	{
		perror("ftok");
		exit(1);
	}

	msgid = msgget(key,IPC_CREAT|IPC_EXCL|0666);
	if(msgid == -1)
	{
		if(errno == EEXIST)
		{
			msgid = msgget(key, 0666);
		}
		else{
			perror("msgget");
			exit(1);
		}
	}


	
	while(1)
	{
		 msgrcv(msgid, &msg_buf, sizeof(msg_buf) - sizeof(long), 1L, 0);
		 printf("recv:%x", msg_buf.text[0]);	

		 if(msg_buf.text[0] == 0x80)
		 {
			 write(dev_uart_fd, &msg_buf.text[0], 1);  //关闭风扇
		 }

		 if(msg_buf.text[0] == 0x81)
		 {
			 write(dev_uart_fd, &msg_buf.text[0], 1);   //一档
		 }

		 if(msg_buf.text[0] == 0x82)
		 {
			 write(dev_uart_fd, &msg_buf.text[0], 1);   //二档
		 }

		 if(msg_buf.text[0] == 0x83)
		 {
			 write(dev_uart_fd, &msg_buf.text[0], 1);   //三当
		 }

		 if(msg_buf.text[0] == 0x90)
		 {
			 write(dev_uart_fd, &msg_buf.text[0], 1);   //蜂鸣器关闭
		 }
		 if(msg_buf.text[0] == 0x91)
		 {
			 write(dev_uart_fd, &msg_buf.text[0], 1);   //打开
		 }
		 if(msg_buf.text[0] == 0x92)
		 {
			 write(dev_uart_fd, &msg_buf.text[0], 1);   //自动报警关闭
		 }

		 if(msg_buf.text[0] == 0x93)
		 {
			 write(dev_uart_fd, &msg_buf.text[0], 1);   //自动报警打开
		 }


		 if(msg_buf.text[0] == 0xa0)
		 {
			 write(dev_uart_fd, &msg_buf.text[0], 1);   //灯关


		 }

		 if(msg_buf.text[0] == 0xa1)
		 {
			 write(dev_uart_fd, &msg_buf.text[0], 1);   //灯开
		 }


		 if(msg_buf.text[0] == 0xbf)
		 {
			 write(dev_uart_fd, &msg_buf.text[0], 1);   //数码管关
		 }



		 if(msg_buf.text[0] == 0xb0)

		 {
			 write(dev_uart_fd, &msg_buf.text[0], 1);   //数码管0
		 }
	 }
	
}

